//
//  Demo3ViewController.m
//  OPenGLES-Demo
//
//  Created by 王潇 on 2019/5/15.
//  Copyright © 2019 王潇. All rights reserved.
//

#import "Demo3ViewController.h"
#import "AGLKVertexAttribArrayBuffer.h"
#import "AGLKContext.h"

@implementation Demo3ViewController


@synthesize baseEffect;
@synthesize vertexBuffer;

/////////////////////////////////////////////////////////////////
// This data type is used to store information for each vertex
typedef struct {
	GLKVector3  positionCoords;
}
SceneVertex;

/////////////////////////////////////////////////////////////////
// Define vertex data for a triangle to use in example
/// 绘制点数组
static const SceneVertex vertices[] = {
	{{-0.5f, -0.5f, 0.0}}, // lower left corner
	{{ 0.5f, -0.5f, 0.0}}, // lower right corner
	{{-0.5f,  0.5f, 0.0}}  // upper left corner
};


/////////////////////////////////////////////////////////////////
// Called when the view controller's view is loaded
// Perform initialization before the view is asked to draw
- (void)viewDidLoad {
	[super viewDidLoad];
	
	// Verify the type of view created automatically by the
	// Interface Builder storyboard
	GLKView *view = (GLKView *)self.view;
	NSAssert([view isKindOfClass:[GLKView class]],
			 @"View controller's view is not a GLKView");
	
	// Create an OpenGL ES 2.0 context and provide it to the
	// view
    /// 创建设置上下文
	view.context = [[AGLKContext alloc]
					initWithAPI:kEAGLRenderingAPIOpenGLES2];
	// Make the new context current
	[AGLKContext setCurrentContext:view.context];
	
	// Create a base effect that provides standard OpenGL ES 2.0
	// shading language programs and set constants to be used for
	// all subsequent rendering
	self.baseEffect = [[GLKBaseEffect alloc] init];
	self.baseEffect.useConstantColor = GL_TRUE;
	self.baseEffect.constantColor = GLKVector4Make(
												   1.0f, // Red
												   1.0f, // Green
												   1.0f, // Blue
												   1.0f);// Alpha
	
	// Set the background color stored in the current context
	((AGLKContext *)view.context).clearColor = GLKVector4Make(
															  0.0f, // Red
															  0.0f, // Green
															  0.0f, // Blue
															  1.0f);// Alpha
	
	// Create vertex buffer containing vertices to draw
	self.vertexBuffer = [[AGLKVertexAttribArrayBuffer alloc] initWithAttribStride:sizeof(SceneVertex)
                                                                 numberOfVertices:sizeof(vertices) /    sizeof(SceneVertex)
                                                                            bytes:vertices
                                                                            usage:GL_STATIC_DRAW];
}


/////////////////////////////////////////////////////////////////
// GLKView delegate method: Called by the view controller's view
// whenever Cocoa Touch asks the view controller's view to
// draw itself. (In this case, render into a frame buffer that
// shares memory with a Core Animation Layer)
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
	[self.baseEffect prepareToDraw];
	
	// Clear back frame buffer (erase previous drawing)
	[(AGLKContext *)view.context clear:GL_COLOR_BUFFER_BIT];
	
	[self.vertexBuffer prepareToDrawWithAttrib:GLKVertexAttribPosition
						   numberOfCoordinates:3
								  attribOffset:offsetof(SceneVertex, positionCoords)
								  shouldEnable:YES];
	
	// Draw triangles using the first three vertices in the
	// currently bound vertex buffer
	[self.vertexBuffer drawArrayWithMode:GL_TRIANGLES
						startVertexIndex:0
						numberOfVertices:3];
}


/////////////////////////////////////////////////////////////////
// Called when the view controller's view has been unloaded
// Perform clean-up that is possible when you know the view
// controller's view won't be asked to draw again soon.
/// 卸载
- (void)viewDidUnload {
	[super viewDidUnload];
	
	// Make the view's context current
	GLKView *view = (GLKView *)self.view;
	[AGLKContext setCurrentContext:view.context];
	
	// Delete buffers that aren't needed when view is unloaded
	self.vertexBuffer = nil;
	
	// Stop using the context created in -viewDidLoad
	((GLKView *)self.view).context = nil;
	[EAGLContext setCurrentContext:nil];
}


@end
